// IniOption.cpp: implementation of the CIniOption class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "IniOption.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CIniOption::CIniOption()
{
    m_strSection  = _T("Options");
    m_strTempPath = _T("C:\\");
}

CIniOption::CIniOption(LPCTSTR strIniPath)
{
    m_strSection  = _T("Options");
    m_strIniPath  = strIniPath;
    m_strTempPath = _T("C:\\");
}

int CIniOption::GetIntValue(LPCTSTR strKey, int nValue)
{
    return GetPrivateProfileInt(m_strSection, strKey, nValue, m_strIniPath);
}

CString CIniOption::GetStringValue(LPCTSTR strKey, CString strValue)
{
    CString strReturn;
    int     nBufferSize(4096);

    GetPrivateProfileString(m_strSection, strKey, strValue, 
                            strReturn.GetBuffer(nBufferSize), nBufferSize,
                            m_strIniPath);
    strReturn.ReleaseBuffer();

    return strReturn;
}

void CIniOption::SetValue(LPCTSTR strKey, const CString &strValue)
{
    WritePrivateProfileString(m_strSection, strKey, strValue, m_strIniPath);
}

void CIniOption::SetValue(LPCTSTR strKey, const int &nValue)
{
    CString strValue;

    strValue.Format(_T("%d"), nValue);

    SetValue(strKey, strValue);
}

BOOL CIniOption::SetIniData(const CString &strIniData)
{
    CFile   file;
    m_strIniPath.Format(_T("%s\\TempIniData.ini"), m_strTempPath);

    if(!file.Open(m_strIniPath, CFile::modeCreate | CFile::modeWrite))
    {
        return FALSE;
    }

    m_strSection = _T("[Temp Options]\r\n");
    file.Write((LPCTSTR)m_strSection, m_strSection.GetLength());
    file.Write((LPCTSTR)strIniData, strIniData.GetLength());
    file.Flush();
    file.Close();

    m_strSection = _T("Temp Options");

    return TRUE;
}

int CIniOption::GetAllKeys(CStringArray& astrKey)
{
    CString strKey;
    TCHAR*  pszBuffer = new TCHAR[32768];
    TCHAR*  pszData = NULL;
    DWORD   dwCopied;
    int     nFind(0);

    astrKey.RemoveAll();

    ::ZeroMemory(pszBuffer, sizeof(TCHAR) * 32768);
    dwCopied = GetPrivateProfileSection(m_strSection, pszBuffer, 
                                        32767, m_strIniPath);
    pszData = pszBuffer;
    while(TRUE)
    {
        strKey.Format(_T("%s"), pszData);
        if(strKey.GetLength() == 0)
            break;
        nFind = strKey.Find(_T("="));
        if(nFind > 0)
        {
            astrKey.Add((LPCTSTR)strKey.Left(nFind).Trim());
        }
        pszData += lstrlen(pszData) + 1;
    }

    delete[] pszBuffer;

    return astrKey.GetSize();
}

int CIniOption::GetAllSections(CStringArray& astrSection)
{
    CString strSection;
    TCHAR*  pszBuffer = new TCHAR[32768];
    TCHAR*  pszData = NULL;
    DWORD   dwCopied;

    astrSection.RemoveAll();

    ::ZeroMemory(pszBuffer, sizeof(TCHAR) * 32768);
    dwCopied = GetPrivateProfileSectionNames(pszBuffer, 32767, m_strIniPath);
    pszData  = pszBuffer;
    while(TRUE)
    {
        strSection.Format(_T("%s"), pszData);
        if(strSection.GetLength() == 0)
        {
            break;
        }

        astrSection.Add((LPCTSTR)strSection);

        pszData += lstrlen(pszData) + 1;
    }

    delete[] pszBuffer;

    return astrSection.GetSize();
}

void CIniOption::DeleteSection(LPCTSTR strSectoin)
{
    WritePrivateProfileSection(strSectoin, 0, m_strIniPath);
}
